package org.springblade.modules.repair.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.base.BaseEntity;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tenant.annotation.TenantIgnore;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.desk.entity.Notice;
import org.springblade.modules.desk.service.INoticeService;
import org.springblade.modules.desk.vo.NoticeVO;
import org.springblade.modules.repair.entity.UserNotice;
import org.springblade.modules.repair.service.IUserNoticeService;
import org.springblade.modules.repair.vo.UserNoticeListVO;
import org.springblade.modules.repair.vo.UserNoticeVO;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Sharry
 * @since 2024-04-06
 */
@RestController
@AllArgsConstructor
@RequestMapping("repair/user-notice")
@Api(value = "用户通知表", tags = "用户通知表接口")
public class UserNoticeController {

	@Autowired
	private IUserNoticeService userNoticeService;

	@Autowired
	private INoticeService noticeService;

	@Autowired
	private IUserService userService;

	@GetMapping("/list")
	@ApiOperation(value = "获取用户通知列表", notes = "获取用户通知列表")
	public R<IPage<UserNoticeListVO>> getUserNoticeList(Query query, String title) {
		// 根据当前用户id获取通知列表
		Long currentUserId = AuthUtil.getUserId();
		IPage<UserNoticeListVO> noticeList = userNoticeService.listByCondition(Condition.getPage(query),currentUserId,title);
		return R.data(noticeList);
	}

	@GetMapping("/unread-count")
	@ApiOperation(value = "获取未读通知数量", notes = "获取未读通知数量")
	public R<Integer> getUnreadCount() {
		// 根据当前用户id获取未读通知数量
		Long currentUserId = AuthUtil.getUserId();
		int count = userNoticeService.count(new LambdaQueryWrapper<UserNotice>()
			.eq(UserNotice::getUserId, currentUserId)
			.eq(UserNotice::getCategory, 0)
			.eq(BaseEntity::getIsDeleted, 0)
		);
		return R.data(count);
	}

	@GetMapping("/detail")
	@ApiOperation(value = "获取用户通知详情", notes = "获取用户通知详情")
	public R<UserNoticeVO> getUserNoticeDetail(Long noticeId) {
		// 0. 数据校验
		if(Func.isEmpty(noticeId)){
			throw new ServiceException("通知id不能为空");
		}
		// 1. 获取当前用户，判断当前用户是否有权限查看该通知：该通知关联表userIds是否包含当前用户id
		Long currentUserId = AuthUtil.getUserId();
		List<UserNotice> userNoticeList = userNoticeService.list(new LambdaQueryWrapper<UserNotice>()
			.eq(UserNotice::getNoticeId, noticeId)
			.eq(BaseEntity::getIsDeleted, 0)
		);
		// 不包含：抛异常，前端使用无权查看缺省页
		if(Func.isEmpty(userNoticeList) || userNoticeList.stream().noneMatch(u -> u.getUserId().equals(currentUserId))){
			throw new ServiceException("无权查看该通知");
		}
		// 2. 获取通知详情
		Notice notice = noticeService.getById(noticeId);
		UserNoticeVO noticeVO = new UserNoticeVO();
		BeanUtil.copyProperties(notice, noticeVO);
		User byId = userService.getById(notice.getCreateUser());
		if(!Func.isNull(byId)){
			noticeVO.setSendName(byId.getRealName());
		}

		// 3. 标记已读
		UserNotice userNotice = userNoticeService.getOne(new LambdaQueryWrapper<UserNotice>()
			.eq(UserNotice::getNoticeId, noticeId)
			.eq(UserNotice::getUserId, currentUserId)
			.eq(BaseEntity::getIsDeleted, 0)
		);
		if(Func.isNotEmpty(userNotice)){
			userNotice.setCategory(1);
			userNoticeService.updateById(userNotice);
		}
		return R.data(noticeVO);
	}

	/**
	 * 更改消息状态为已读 全部
	 * @param
	 * @return
	 */
	@TenantIgnore
	@PutMapping("/read/all")
	public R<Boolean> readAll(){
		return R.status(userNoticeService.readAll(AuthUtil.getUserId()));
	}


}
